﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SharingCenter.Web.Classes;

namespace SharingCenter.Web.Admin.Administrator
{
    
    public partial class Default : AdminPageBase
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            btnAssign.Click += new EventHandler(btnAssign_Click);
            ListView1.ItemCommand += new EventHandler<ListViewCommandEventArgs>(ListView1_ItemCommand);
        }

        void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            string un = e.CommandArgument.ToString();
            Roles.RemoveUserFromRole(un, WebConsts.SITE_ADMIN_ROLENAME);
            DisplayAssignedAdmins();
        }

        void btnAssign_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim().ToLower();
            try
            {
                if (!Roles.RoleExists(WebConsts.SITE_ADMIN_ROLENAME))
                {

                    Roles.CreateRole(WebConsts.SITE_ADMIN_ROLENAME);
                }

                if (!Roles.IsUserInRole(userName, WebConsts.SITE_ADMIN_ROLENAME))
                {
                    Roles.AddUserToRole(userName, WebConsts.SITE_ADMIN_ROLENAME);
                }
            }
            catch { }

            DisplayAssignedAdmins();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            DisplayAssignedAdmins();
        }

        protected void DisplayAssignedAdmins()
        {
            try
            {
                if (!Roles.RoleExists(WebConsts.SITE_ADMIN_ROLENAME))
                {

                    Roles.CreateRole(WebConsts.SITE_ADMIN_ROLENAME);
                }
                string[] users = Roles.GetUsersInRole(WebConsts.SITE_ADMIN_ROLENAME);
                ListView1.DataSource = users;
                ListView1.DataBind();
            }
            catch { Response.Write("数据错误。"); }
        }
    }
}